<template>
  <div class="pagination-container">
    <el-table :data="tableData" style="width: 100%" stripe>
      <el-table-column prop="traceId" label="traceId" width="180" />
      <el-table-column prop="userName" label="userName" width="180" />
      <el-table-column prop="ip" label="IP地址" width="180" />
      <el-table-column prop="params" label="参数" width="180">
        <template #default="{ row }">
          <el-tooltip content="查看详情" placement="top">
            <div class="ellipsis" :title="row.params">{{ row.params }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="className" label="类名" />
      <el-table-column prop="operateTime" label="调用时间" width="180" />
      <el-table-column prop="method" label="调用方法" />
      <el-table-column prop="result" label="执行结果" width="120">
        <template #default="{ row }">
          <el-tag :type="row.result === 'success' ? 'success' : 'danger'">
            {{ row.result }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="errorMsg" label="错误信息" width="180">
        <template #default="{ row }">
          <el-tooltip content="查看详情" placement="top">
            <div class="ellipsis" :title="row.errorMsg">{{ row.errorMsg || '-' }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="costTime" label="运行时长(ms)" width="120" />
    </el-table>

    <div class="pagination-footer">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { getLogsList } from '@/api/logs.js'

// 表格数据
const tableData = ref([])
// 分页参数
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
// 加载状态
const loading = ref(false)
// 搜索条件
const searchParams = ref({})

// 获取日志列表
const getLogs = async () => {
  loading.value = true


  const params = {
    pageNum: currentPage.value,
    pageSize: pageSize.value,
  }

  const res = await getLogsList(params)

  tableData.value = res.data.rows || []
  total.value = res.data.total || 0
  loading.value = false

}

// 监听分页变化
const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  currentPage.value = 1 // 切换每页条数时重置到第一页
  getLogs()
}

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
  getLogs()
}
//日期格式化
const formattedDateTime = computed((dateData) => {
  const [year, month, day, hour, minute, second] = dateData;
  const date = new Date(year, month - 1, day, hour, minute, second);
  return format(date, 'yyyy-MM-dd HH:mm:ss'); // 或使用其他方法
});

// 监听搜索条件变化
watch(searchParams, () => {
  currentPage.value = 1 // 搜索条件变化时重置到第一页
  getLogs()
}, { deep: true })

onMounted(() => {
  getLogs()
})
</script>

<style scoped>
.pagination-container {
  margin: 20px;
}

.pagination-footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
</style>

